package com.power.using.mytoutiao.module.news;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import com.google.android.material.tabs.TabLayout;
import com.power.using.mytoutiao.R;
import com.power.using.mytoutiao.adapter.base.BasePagerAdapter;
import com.power.using.mytoutiao.bean.news.NewsChannelBean;
import com.power.using.mytoutiao.constant.Constant;
import com.power.using.mytoutiao.database.dao.NewsChannelDao;
import com.power.using.mytoutiao.module.base.BaseListFragment;
import com.power.using.mytoutiao.module.news.article.NewsArticleView;
import com.power.using.mytoutiao.module.wenda.article.WendaArticalView;
import com.power.using.mytoutiao.util.RxBus;
import com.power.using.mytoutiao.util.SettingUtil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import io.reactivex.Observable;
import io.reactivex.functions.Consumer;

/**
 * Created by 聪明一只哈 on 2020/4/3.
 * Time: 2020/4/3  14:02
 */
public class NewsTabLayout extends Fragment {

    public static final String TAG="NewsTabLayout";
    private static NewsTabLayout instance;
    private ViewPager viewPager;
    private LinearLayout linearLayout;
    private NewsChannelDao dao = new NewsChannelDao();
    private List<Fragment> fragmentList;
    private List<String> titleList;
    private Map<String, Fragment> map = new HashMap<>();
    private BasePagerAdapter adapter;
    private Observable<Object> observable;

    public static NewsTabLayout getInstance(){
        if(instance==null){
            instance = new NewsTabLayout();
        }
        return instance;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        View view=inflater.inflate(R.layout.fragment_news_tab,container,false);
        initView(view);
        initData();
        return view;
    }


    @Override
    public void onResume() {
        super.onResume();
        linearLayout.setBackgroundColor(SettingUtil.getInstance().getColor());
    }

    private void initView(View view) {
        TabLayout tab_layout=view.findViewById(R.id.tab_layout_news);
        viewPager = view.findViewById(R.id.view_pager_news);

        tab_layout.setupWithViewPager(viewPager);
        tab_layout.setTabMode(TabLayout.MODE_SCROLLABLE);
        ImageView add_chanel_iv = view.findViewById(R.id.add_channel_iv);
        add_chanel_iv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });
        linearLayout = view.findViewById(R.id.header_layout);
        linearLayout.setBackgroundColor(SettingUtil.getInstance().getColor());

    }


    private void initData() {

        initTabs();
        adapter = new BasePagerAdapter(getChildFragmentManager(),fragmentList,titleList);
        viewPager.setAdapter(adapter);
        viewPager.setOffscreenPageLimit(15);

        observable = RxBus.getInstance().register(NewsTabLayout.TAG);
        observable.subscribe(new Consumer<Object>() {
            @Override
            public void accept(Object o) throws Exception {
                boolean isRefresh= (boolean) o;
                if(isRefresh){
                    initTabs();
                    adapter.recreateItems(fragmentList,titleList);
                }
            }
        });


    }

    private void initTabs() {
        
        List<NewsChannelBean> channelList= dao.query(1);
        fragmentList = new ArrayList<>();
        titleList = new ArrayList<>();
        if(channelList.size()==0){
            dao.addInitData();
            channelList=dao.query(Constant.NEWS_CHANNEL_ENABLE);
        }
        
        for (NewsChannelBean bean: channelList) {
            Fragment fragment=null;
            String channelId = bean.getChannelId();
            switch(channelId){
                case "question_and_answer":
                    if(map.containsKey(channelId)){
                        fragmentList.add(map.get(channelId));
                    }else{
                        fragment= WendaArticalView.newInstance();
                        fragmentList.add(fragment);
                    }
                    break;

                default:
                    if(map.containsKey(channelId)){
                        fragmentList.add(map.get(channelId));
                    }else{
                        fragment= NewsArticleView.newInstance();
                        fragmentList.add(fragment);
                    }

                    break;
            }

            titleList.add(bean.getChannelName());

            if(fragment!=null){
                map.put(channelId,fragment);
            }
        }

    }

    public void onDoubleClick(){
        if(titleList!=null&&titleList.size()>0&&fragmentList!=null&&fragmentList.size()>0){
            int item=viewPager.getCurrentItem();
            ((BaseListFragment)fragmentList.get(item)).onRefresh();
        }
    }

    @Override
    public void onDestroy() {

        RxBus.getInstance().unregister(NewsTabLayout.TAG,observable);
        if(instance!=null){
            instance=null;
        }
        super.onDestroy();


    }
}
